package cn.iocoder.yudao.module.reading.dal.mysql.follow;

import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.reading.dal.dataobject.follow.UserFollowDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * 用户关注 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface UserFollowMapper extends BaseMapperX<UserFollowDO> {

    /**
     * 根据关注者和被关注者查询关注记录
     *
     * @param followerId 关注者用户ID
     * @param followeeId 被关注者用户ID
     * @return 关注记录
     */
    default UserFollowDO selectByFollowerAndFollowee(Long followerId, Long followeeId) {
        return selectOne(new LambdaQueryWrapperX<UserFollowDO>()
                .eq(UserFollowDO::getFollowerId, followerId)
                .eq(UserFollowDO::getFolloweeId, followeeId));
    }

    /**
     * 根据关注者ID查询关注列表
     *
     * @param followerId 关注者用户ID
     * @param status     关注状态（可选）
     * @param limit      限制数量
     * @return 关注列表
     */
    default List<UserFollowDO> selectByFollowerId(Long followerId, String status, Integer limit) {
        return selectList(new LambdaQueryWrapperX<UserFollowDO>()
                .eq(UserFollowDO::getFollowerId, followerId)
                .eqIfPresent(UserFollowDO::getStatus, status)
                .orderByDesc(UserFollowDO::getCreateTime)
                .last(limit != null ? "LIMIT " + limit : ""));
    }

    /**
     * 根据被关注者ID查询粉丝列表
     *
     * @param followeeId 被关注者用户ID
     * @param status     关注状态（可选）
     * @param limit      限制数量
     * @return 粉丝列表
     */
    default List<UserFollowDO> selectByFolloweeId(Long followeeId, String status, Integer limit) {
        return selectList(new LambdaQueryWrapperX<UserFollowDO>()
                .eq(UserFollowDO::getFolloweeId, followeeId)
                .eqIfPresent(UserFollowDO::getStatus, status)
                .orderByDesc(UserFollowDO::getCreateTime)
                .last(limit != null ? "LIMIT " + limit : ""));
    }

    /**
     * 统计关注数量
     *
     * @param followerId 关注者用户ID
     * @param status     关注状态（可选）
     * @return 关注数量
     */
    default Long countByFollowerId(Long followerId, String status) {
        return selectCount(new LambdaQueryWrapperX<UserFollowDO>()
                .eq(UserFollowDO::getFollowerId, followerId)
                .eqIfPresent(UserFollowDO::getStatus, status));
    }

    /**
     * 统计粉丝数量
     *
     * @param followeeId 被关注者用户ID
     * @param status     关注状态（可选）
     * @return 粉丝数量
     */
    default Long countByFolloweeId(Long followeeId, String status) {
        return selectCount(new LambdaQueryWrapperX<UserFollowDO>()
                .eq(UserFollowDO::getFolloweeId, followeeId)
                .eqIfPresent(UserFollowDO::getStatus, status));
    }

    /**
     * 检查是否互相关注
     *
     * @param userId1 用户1ID
     * @param userId2 用户2ID
     * @return 是否互相关注
     */
    default boolean isMutualFollow(Long userId1, Long userId2) {
        UserFollowDO follow1 = selectByFollowerAndFollowee(userId1, userId2);
        UserFollowDO follow2 = selectByFollowerAndFollowee(userId2, userId1);
        
        return follow1 != null && "active".equals(follow1.getStatus()) &&
               follow2 != null && "active".equals(follow2.getStatus());
    }

    /**
     * 批量检查关注状态
     *
     * @param followerId  关注者用户ID
     * @param followeeIds 被关注者用户ID列表
     * @return 已关注的用户ID列表
     */
    default List<Long> selectFollowedUserIds(Long followerId, List<Long> followeeIds) {
        List<UserFollowDO> follows = selectList(new LambdaQueryWrapperX<UserFollowDO>()
                .eq(UserFollowDO::getFollowerId, followerId)
                .in(UserFollowDO::getFolloweeId, followeeIds)
                .eq(UserFollowDO::getStatus, "active"));
        
        return follows.stream().map(UserFollowDO::getFolloweeId).toList();
    }

    /**
     * 根据关注者和被关注者删除关注记录
     *
     * @param followerId 关注者用户ID
     * @param followeeId 被关注者用户ID
     * @return 删除数量
     */
    default int deleteByFollowerAndFollowee(Long followerId, Long followeeId) {
        return delete(new LambdaQueryWrapperX<UserFollowDO>()
                .eq(UserFollowDO::getFollowerId, followerId)
                .eq(UserFollowDO::getFolloweeId, followeeId));
    }

    /**
     * 更新关注状态
     *
     * @param followerId 关注者用户ID
     * @param followeeId 被关注者用户ID
     * @param status     新状态
     * @return 更新数量
     */
    default int updateStatusByFollowerAndFollowee(Long followerId, Long followeeId, String status) {
        UserFollowDO updateObj = new UserFollowDO();
        updateObj.setStatus(status);
        
        return update(updateObj, new LambdaQueryWrapperX<UserFollowDO>()
                .eq(UserFollowDO::getFollowerId, followerId)
                .eq(UserFollowDO::getFolloweeId, followeeId));
    }

}
